import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    //  路由的动态加载
    component: () => import('../views/Home.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/detail',
    name: 'Detail',
    component: () => import('../views/Detail')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 登录拦截
router.beforeEach((to, from, next) => {

  const token = localStorage.getItem('token');

  if (!token && to.path !== '/login') {
    next('/login')
  } else {
    next()
  }

})

export default router
